<?php

namespace common\logics\workflow;

use Yii;
use common\models\shop\ShopServiceContract;
use common\models\shop\ShopServiceContractLog;
use common\models\shop\ShopServiceContractWorkflow;

/**
 * 签约服务流程自动执行逻辑器
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class ContractServiceWorkflowLogic extends \common\base\Model {

    /**
     * @var ShopServiceContract
     */
    public $apply;

    /**
     * @var ShopServiceContractWorkflow 
     */
    public $workflow;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            ['apply', 'required'],
            ['workflow', 'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function execute() {
        if (!$this->validate()) {
            return false;
        }
        $apply = $this->apply;
        $workflow = $this->workflow;

        //无后续流程，执行终止流程
        if (!($workflow->next_id && $workflow->fallback_id)) {
            Yii::error([
                '无后续流程，执行终止流程',
                $workflow->id,
                $workflow->next_id,
                $workflow->fallback_id,
            ]);
            return $workflow->finish($apply);
        }

        //单一流程
        if ($workflow->next_id == $workflow->fallback_id) {
            if (!$workflow->role_id) {
                //执行单一流程
                if ($workflow->index == $workflow->next_id) {
                    //暂停流程
                    return $workflow->wait($apply);
                }
                //自动下一流程
                if ($workflow->duration > 0 && $workflow->next_id == $workflow->expire_id) {
                    //执行超时自动任务流
                    $workflow->pushExpireJob($apply);
                    return $apply;
                }
                //自动下一流程 - 立即执行
                return $workflow->next($apply);
            } else {
                //人为审核操控流程
                //等待人工操作
            }
        }

        //特殊操作，商家支付保证金
        if ($workflow->role_id === ShopServiceContractWorkflow::ROLE_STORE) {
            if ($workflow->bondPayment($apply, $workflow)) {
                Yii::error([
                    '自动支付完成',
                    $workflow->id,
                    $apply->attributes,
                ]);
                return $workflow->next($apply);
            }
        }

        //特殊操作，商家支付保证金财务审核
        if ($workflow->role_id === ShopServiceContractWorkflow::ROLE_FINANCE) {
            if ($workflow->bondPaymentAudit($apply, $workflow)) {
                Yii::error([
                    '自动支付完成',
                    $workflow->id,
                    $apply->attributes,
                ]);
                return $workflow->next($apply);
            }
        }

        //超时流程
        if ($workflow->duration && $workflow->expire_id) {
            $retry = true;
            //执行超时自动任务流
            if ($workflow->max_retry) {
                $retries = ShopServiceContractLog::findRetryTimes($apply->id, $workflow->id);
                if ($retries > $workflow->max_retry) {
                    $retry = false;
                }
            }
            if ($retry) {
                //执行超时自动任务流
                $workflow->pushExpireJob($apply);
            }
        }
        return $apply;
    }

}
